package com.masterclient.app.modules.firstpager.fragments;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.masterclient.app.App;
import com.masterclient.app.BaseActivity;
import com.masterclient.app.R;
import com.masterclient.app.base.MessageCallBack;
import com.eoner.baselibrary.bean.firstpager.AwesomeDetailBean;
import com.masterclient.app.modules.firstpager.fragments.service.FirstPagerService;
import com.eoner.baselibrary.utils.ScreenUtils;
import com.masterclient.app.widget.CircleTransform;
import com.masterclient.app.widget.WrapContentHeightViewPager;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 作者：sean
 * 创建时间：2017/6/19 10:08
 * 功能说明：好物详情页
 */

public class AwesomeGoodsDetailActivity extends BaseActivity {

    @BindView(R.id.rl_back)
    RelativeLayout rlBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.ll_more)
    LinearLayout llMore;
    @BindView(R.id.rl_head)
    RelativeLayout rlHead;
    @BindView(R.id.viewPager)
    WrapContentHeightViewPager viewPager;
    @BindView(R.id.tv_indicator)
    TextView tvIndicator;
    @BindView(R.id.iv_brand_logo)
    ImageView ivBrandLogo;
    @BindView(R.id.tv_brand_name)
    TextView tvBrandName;
    @BindView(R.id.webView)
    WebView webView;
    @BindView(R.id.swipeLayout)
    SwipeRefreshLayout swipeLayout;
    private FirstPagerService firstPagerService;
    String id;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_awesome_good_detail);
        ButterKnife.bind(this);
        firstPagerService = new FirstPagerService();
        id = getBundleString("id");
        getAwesomeDetail(id);
        swipeLayout.setOnRefreshListener(()->getAwesomeDetail(id));
    }

    private void getAwesomeDetail(String id) {
        firstPagerService.getAwesomeDetail(id, new MessageCallBack<AwesomeDetailBean.DataBean>() {
            @Override
            public void onSuccess(@NonNull AwesomeDetailBean.DataBean dataBean) {
                if(swipeLayout.isRefreshing()){
                    swipeLayout.setRefreshing(false);
                }
                AwesomePagerAdapter pagerAdapter = new AwesomePagerAdapter(dataBean.getSh_image());
                viewPager.setAdapter(pagerAdapter);

                App.picasso.load(dataBean.getSh_brand_logo()).resize(ScreenUtils.dp2px(40),ScreenUtils.dp2px(40)).transform(new CircleTransform()).into(ivBrandLogo);
                tvBrandName.setText(dataBean.getSh_name());
                webView.loadUrl(dataBean.getSh_url());
                tvTitle.setText(dataBean.getSh_name());
                tvBrandName.setText(dataBean.getSh_brand_name());

                viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
                    @Override
                    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

                    }

                    @Override
                    public void onPageSelected(int position) {
                        tvIndicator.setText((position+1)+"/"+dataBean.getSh_image().size());
                    }

                    @Override
                    public void onPageScrollStateChanged(int state) {

                    }
                });
            }

            @Override
            public void onError(String s) {
                if(swipeLayout.isRefreshing()){
                    swipeLayout.setRefreshing(false);
                }
            }

            @Override
            public void onFail() {
                if(swipeLayout.isRefreshing()){
                    swipeLayout.setRefreshing(false);
                }
            }
        });
    }

    @OnClick(R.id.ll_more)void onMoreClick(){

    }
    @OnClick(R.id.rl_back) void onClickBack(){
        finish();
    }

    private class AwesomePagerAdapter extends PagerAdapter {
        private List<AwesomeDetailBean.DataBean.ShImageBean> list;

        public AwesomePagerAdapter(List<AwesomeDetailBean.DataBean.ShImageBean> list) {
            this.list = list;
        }

        @Override
        public int getCount() {
            return list == null ? 0 : list.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            ImageView imageView = new ImageView(AwesomeGoodsDetailActivity.this);
            imageView.setScaleType(ImageView.ScaleType.FIT_XY);
            imageView.setAdjustViewBounds(true);
            ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewPager.LayoutParams.MATCH_PARENT, ViewPager.LayoutParams.WRAP_CONTENT);
            imageView.setLayoutParams(params);
            App.picasso.load(list.get(position).getSh_image()).fit().error(R.mipmap.defaults_1).into(imageView);
            container.addView(imageView);
            return imageView;
        }
    }
}
